package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgMarketDataMapper;
import com.csun.dsms.mapper.HgProjectDataMapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgMarketDataService;
import com.csun.dsms.service.HgProjectDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgProjectDataServiceImpl extends BaseService<HgProjectData>
        implements HgProjectDataService {

    @Resource
    private HgProjectDataMapper hgProjectDataMapper;
    
    @Override
    public List<HgProjectData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgProjectDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgProjectDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importProjectData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgProjectData hgProjectData = new HgProjectData();
                hgProjectData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgProjectData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgProjectData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgProjectData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgProjectData.setIndustry(Convert.toStr(excelMap.get("工业")));
                hgProjectData.setTotalNum(Convert.toInt(excelMap.get("总数量")));
                hgProjectData.setRedNum(Convert.toInt(excelMap.get("红色数字")));
                hgProjectData.setBlackNum(Convert.toInt(excelMap.get("黑色数字")));
                hgProjectData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgProjectData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgProjectData.setApproved(Convert.toBool(excelMap.get("是否审核")));
                hgProjectData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgProjectData.setCreatedTime(new Date());
                hgProjectDataMapper.insert(hgProjectData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入建设清单成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }


}
